package com.cxrj.zhsq.userManage.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.cxrj.zhsq.annotation.SystemControllerLog;
import com.cxrj.zhsq.userManage.service.UserService;

@RestController
public class UserController {

	@Resource
	private UserService userService;

	/**
	 * 登录
	 * 
	 * @param username
	 *            账号
	 * @param password
	 *            密码
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "user_login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> login(String username, String password, HttpServletRequest request) {
		return userService.login(username, password, request);

	}

	/**
	 * 登录(跳转)
	 * 
	 * @param username
	 *            账号
	 * @param password
	 *            密码
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "user_login_jump", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ModelAndView user_login_jump(String username, String password, HttpServletRequest request) {
		Map<String, Object> resultMap = userService.login(username, password, request);
		resultMap.put("roleId", resultMap.get("roleId"));
		return new ModelAndView("WEB-INF/jsp/index", "map", resultMap);

	}

	/**
	 * @Author: WangBo
	 * @Description: 登出
	 * @param
	 * @Date: 9:14 2017/11/23
	 */
	@RequestMapping("logout")
	public ModelAndView logout(HttpSession session) {
		session.invalidate();
		return new ModelAndView("WEB-INF/jsp/login");
	}

	/**
	 * 分页查询用户信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectBackstageUser", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> selectAppUser(HttpServletRequest request, HttpServletResponse response) {
		return userService.selectAppUser(request, response);
	}

	/**
	 * 添加用户
	 * 
	 * @return
	 */
	@RequestMapping(value = "insertUser", method = RequestMethod.POST)
	@SystemControllerLog(description = "添加用户")
	public Map<String, Object> insertUser(@RequestParam Map<String, Object> map, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<>();
		try {
			userService.insertUser(map, request);
			resultMap.put("result", 1);
		} catch (Exception e) {
			resultMap.put("result", -1);
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * @Description: 修改菜单
	 * @param map
	 *            参数map
	 */
	@RequestMapping(value = "updateUser", method = RequestMethod.POST)
	@SystemControllerLog(description = "修改用户")
	public Map<String, Object> updateUser(@RequestParam Map<String, Object> map, HttpSession session) {
		Map<String, Object> resultMap = new HashMap<>();
		try {
			userService.updateUserInfo(map);
			resultMap.put("result", 1);
		} catch (Exception e) {
			resultMap.put("result", -1);
		}
		return resultMap;
	}

	/**
	 * 根据ID删除用户
	 * 
	 * @return
	 */
	@RequestMapping(value = "deleteUser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@SystemControllerLog(description = "删除用户")
	public Map<String, Object> deleteUser(String userId) {
		Map<String, Object> resultMap = new HashMap<>();
		try {
			userService.deleteUserById(userId);
			resultMap.put("result", 1);
		} catch (Exception e) {
			resultMap.put("result", -1);
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * @Description: 修改菜单
	 * @param map
	 *            参数map
	 */
	@RequestMapping(value = "updatePass", method = RequestMethod.POST)
	public Map<String, Object> updatePass(String oldPass, String newPass, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<>();
		try {
			resultMap = userService.updatePass(oldPass, newPass, request);
		} catch (Exception e) {
			resultMap.put("result", 0);
		}
		return resultMap;
	}

	/**
	 * 验证账号是否使用
	 * 
	 * @return
	 */
	@RequestMapping(value = "checkUserAccount", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> checkUserAccount(String userAccount) {

		return userService.selectUserAccount(userAccount);
	}

	/**
	 * 查询用户信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectUsers", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> selectUsers(HttpServletRequest request, HttpServletResponse response) {
		return userService.selectUsersPager(request, response);
	}

	/**
	 * 根据ID查询用户
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectUserById", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> selectRoleById(String userId) {
		return userService.selectUserById(userId);
	}

}
